﻿using BLL;
using System;
using System.Web.UI.WebControls;

namespace Thksoft.Module.DataDetail
{
    public partial class HaveDoneProject : BaseUI.BasePage
    {
        private Lc_TransactionBLL transactionBLL = new Lc_TransactionBLL();
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["WHERE"] = null;
                //初始化公司信息
                btnSelect_Click(sender, e);
            }
        }

        private void BindData(bool iffirst)
        {
            if (iffirst)
            {
                PageIng.CurrentPage = 1;
            }
            string where = null;
            if (ViewState["WHERE"] != null)
            {
                where = ViewState["WHERE"] as string;
            }
            if (iffirst)
            {
                PageIng.RowsCount = transactionBLL.GetCountYBSY(lognUser.USER_ID.ToString(), where);
                //  rowcount = transactionBLL.GetCountYBSY(lognUser.USER_ID.ToString(), where);
            }
            var orderByStr = "B.N_BUSINESS_OPERATOR_TIME DESC";
            if (ViewState["SortOrder"] != null)
            {
                orderByStr = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
            }

            var dt = transactionBLL.GetListByPageYBSY(lognUser.USER_ID.ToString(), where, orderByStr, PageIng.CurrentPage, PageIng.RowsPerPage);
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string where = "";
            if (FORM_COMPANY_ID.Value == -1)
            {
                where = " AND B.N_COMPANY_ID IN (" + companyBLL.GetCompanyFatherAndChildByID(CurrentUserCompanyID) + ")";
            }
            else
            {
                where = " AND B.N_COMPANY_ID = " + FORM_COMPANY_ID.Value.ToString();
            }
            if (FORM_STATE.SelectedValue != "-1")
            {
                where += " AND B.N_BUSINESS_STATE = " + FORM_STATE.SelectedValue;
            }
            if (FORM_BEGIN_DATE.Text != "")
            {
                where += " AND B.N_BUSINESS_OPERATOR_TIME >= TO_DATE('" + FORM_BEGIN_DATE.Text + "','YYYY-MM-DD HH24:MI:SS')";
            }
            if (FORM_OVER_DATE.Text != "")
            {
                where += " AND B.N_BUSINESS_OPERATOR_TIME <= TO_DATE('" + FORM_OVER_DATE.Text + "','YYYY-MM-DD HH24:MI:SS')";
            }
            ViewState["WHERE"] = where;

            BindData(true);
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string buid = ((Label)e.Row.FindControl("lblBuid")).Text;
                if (buid == "0")
                {
                    ((Panel)e.Row.FindControl("Panel2")).Visible = false;
                }
            }
        }

        /// <summary>
        /// GridView标题列排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindData(true);
        }
    }
}